package com.iemes.controller.transport;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.TransporTrecordFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.transport.TransporTrecordMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/transporTrecord/")
@Controller
public class TransporTrecordController extends BaseController{
	@Inject
	private TransporTrecordMapper transporTrecordMapper;
	//查看车队档案列表
	@RequestMapping("/examinetransporTrecord")
	public String examineTransport(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH+"/transport/trecord/transpor_list";
	}
	//车队档案分页
	@ResponseBody
	@RequestMapping("/transportrecord_findbyPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		TransporTrecordFormMap transporTrecordFormMap=getFormMap(TransporTrecordFormMap.class);
		List<Map<String, Object>> transporTrecordFormMaps=transporTrecordMapper.getAllInfo(transporTrecordFormMap);
		LuogicPaging(pageNow, pageSize, transporTrecordFormMaps);
		return pageView;
	}
	//修改车队档案
	@RequestMapping("/transport_edit")
	public String tranup(Model model){
		String id = getPara("id");
		//获取对象
		if(Common.isNotEmpty(id))
		{
			model.addAttribute("tranedit", baseMapper.findbyFrist("id", id, TransporTrecordFormMap.class));
	
		}
		return Common.BACKGROUND_PATH + "/transport/trecord/transport_edit";
	}
	@ResponseBody
	@RequestMapping(value="/transport_editEntity")
	@SystemLog(module="车队档案",methods="档案-编辑档案")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() {
		TransporTrecordFormMap transporTrecordFormMap = getFormMap(TransporTrecordFormMap.class);
		System.out.println(transporTrecordFormMap);
		try {
			transporTrecordMapper.editEntity(transporTrecordFormMap);
		} catch (Exception e) {
			return "false";
		}
		return "success";
	}
	
	
	//添加车队档案
	@RequestMapping("/transport_add")
	public String addUI(Model model){
		return Common.BACKGROUND_PATH + "/transport/trecord/tranport_add";
	}
	@ResponseBody
	@RequestMapping("/transport_addEntity")
	@SystemLog(module="车队档案",methods="档案-新增档案")
	public String addEntity(){
		TransporTrecordFormMap transporTrecordFormMap=getFormMap(TransporTrecordFormMap.class);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		transporTrecordFormMap.put("byuser_id", userFormMap.getInt("id"));
		//transporTrecordFormMap.set("signDate", DateUtils.getStringDateTime());
		try {
			transporTrecordMapper.addEntity(transporTrecordFormMap);
		} catch (Exception e) {
			return "false";
		}
		return "success";
	}
	//删除车队档案
			@ResponseBody
			@RequestMapping("transporTrecord_deleteEntity")
			@Transactional(readOnly=false)//需要事务操作必须加入此注解
			@SystemLog(module="删除车队档案",methods="pod按钮-删除pod按钮")//凡需要处理业务逻辑的.都需要记录操作日志
			public String deleteEntity() throws Exception {
				TransporTrecordFormMap transporTrecordFormMap=getFormMap(TransporTrecordFormMap.class);
				String[] ids = getParaValues("ids");
				String arr=ids[0];
				String[] str=arr.split(",");
				for(int i=0;i<str.length;i++){
					transporTrecordFormMap.set("status", 2);
					transporTrecordFormMap.put("id", str[i]);
					transporTrecordMapper.editEntity(transporTrecordFormMap);
				}
				
				return "success";
			}
}